package com.societyGames.faceExercises
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class FaceDataRender extends Sprite
{
  private static const textOrder:Vector.<String> = Vector.<String>(["Sex", /*"Eyes",*/ "Glasses", "Hair", /*"Mustache",*/ "Hat", "Age"]);

  [Embed(source="../../../../../FaceExercises/assets/Point.png")]
  private static const pointImageClass:Class;

  private var _faceData:FaceData;
  private var _faceImage:FaceImage;
  private var _textField:TextField;
  private var _background:Sprite;
  private var _pointImage:DisplayObject;

  public function FaceDataRender(faceData:FaceData, shouldShowName:Boolean)
  {
    this._faceData = faceData;

    //Create background
    this._background = new Sprite();
    this.addChild(this._background);

    //Create the face graphic
    _faceImage = new FaceImage(faceData);
    this.addChild(_faceImage);

    //Create the textfield
    this._textField = new TextField();
    this._textField.multiline = true;
    this._textField.autoSize = TextFieldAutoSize.CENTER;
    setText(this._textField, _faceData);
    this.addChild(this._textField);

    //Create point image
    this._pointImage = new pointImageClass();
    this._pointImage.scaleX = 2.0;
    this._pointImage.scaleY = 2.0;
    this.addChild(this._pointImage);

    //FaceImage
    var currentY:Number = 0;
    _faceImage.x = (this.width - _faceImage.width) / 2;
    _faceImage.y = currentY;
    currentY += _faceImage.height;

    //Text
    this._textField.x = (this.width - this._textField.textWidth) / 2;
    this._textField.y = currentY;
    currentY += _textField.height;

    //Background
    this._background.graphics.lineStyle(1, 0x000000);
    this._background.graphics.beginFill(0xF0F0F0, 1);
    this._background.graphics.drawRoundRect(0, 0, this.width, this.height, 5, 5);

    //Point
    _pointImage.x = (this.width - _pointImage.width) / 2;
    _pointImage.y = _faceImage.y + _faceImage.height;
    _pointImage.visible = false;

  }

  public function get pointImage():DisplayObject
  {
    return _pointImage;
  }

  public function showSad():void
  {
    _faceImage.showSad();
  }

  private function setText(textField:TextField, faceData:FaceData):void
  {
    textField.text = "Name: " + faceData.name;
    for each (var key:String in FaceDataRender.textOrder)
    {
      var value:String = faceData.getTrait(key);
      if (value != null)
      {
        textField.appendText("\n" + key + ": " + value);
      }
    }
  }
}
}